package com.example.textview;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingComponent;
import androidx.databinding.DataBindingUtil;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.textview.databinding.ActivityAirAPIBinding;
import com.example.textview.util.HttpOK3;
import com.example.textview.util.Weather;
import com.example.textview.weather.WeatherView;

public class AirAPIActivity extends AppCompatActivity {

ActivityAirAPIBinding binding;

    Handler handler;
    Weather weather;
    HttpOK3 httpOK3;
    ImageView mimage;

   WeatherView weatherView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      //  setContentView(R.layout.activity_air_a_p_i);

        binding = DataBindingUtil.setContentView(this,R.layout.activity_air_a_p_i);
        weather=new Weather();
        binding.setData(weather);
        binding.setLifecycleOwner(this);

        mimage=findViewById(R.id.f1);

        weatherView=findViewById(R.id.weaview);

        handler= new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                switch (msg.what){

                    case 1:

                        Glide.with(AirAPIActivity.this).load(weather.getData1().getValue().get(0).getWeather_pic().getValue()).listener(new RequestListener<Drawable>() {
                            @Override
                            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {

                                Log.i("HTTPOK", "加载失败 errorMsg:"+(e!=null?e.getMessage():"null"));
                                return false;
                            }

                            @Override
                            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                                Log.i("HTTPOK", "成功  Drawable Name:"+resource.getClass().getCanonicalName());
                                return false;
                            }
                        }).into(mimage);


                        Glide.with(AirAPIActivity.this).load(weather.getData1().getValue().get(1).getWeather_pic().getValue()).into(binding.f2);


                        Glide.with(AirAPIActivity.this).load(weather.getData1().getValue().get(2).getWeather_pic().getValue()).into(binding.f3);

                        weatherView.setData(weather);
                        break;
                }
            }
        };



        httpOK3=new HttpOK3();

        httpOK3.setOnResponseCallBack(new HttpOK3.OnResponseCallBack() {
            @Override
            public void onRecive(String STR) {


               weather.setJsonData(STR);

               Message message=new Message();
               message.what=1;
               handler.sendMessage(message);

            }
        });
    }





    public void click(View V)
    {
       // httpOK3.get("https://tianqiapi.com/api?version=v1&appid=18779189&appsecret=HY00vp8x&city=松江");

        httpOK3.get1("https://weather01.market.alicloudapi.com/area-to-weather?area=诸暨");



       // httpOK3.post("https://aliv13.data.moji.com/whapi/json/alicityweather/briefforecast6days");

    }

}